package com.zhanbo.zhanbowymusic.main.featured;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;

import com.mwx.baselib.base.BaseBindingAdapter;
import com.mwx.baselib.bean.RankingListBean;
import com.mwx.baselib.net.BaseResponses;
import com.mwx.baselib.net.DefaultObserver;
import com.mwx.baselib.net.NetworkManager;
import com.mwx.baselib.utils.MLog;
import com.mwx.baselib.view.Divider;
import com.mwx.baselib.view.DividerItemDecoration;
import com.zhanbo.zhanbowymusic.R;
import com.zhanbo.zhanbowymusic.databinding.FragmentRankingListBinding;
import com.mwx.baselib.base.BaseFragment;
import com.zhanbo.zhanbowymusic.main.bean.RanKingListDetailsEBBean;
import com.zhanbo.zhanbowymusic.main.featured.adapter.FeaturedGuanFangAdapter;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 排行榜
 */
public class RankingListFragment extends BaseFragment<FragmentRankingListBinding> implements View.OnClickListener {


    private FeaturedGuanFangAdapter featuredGuanFangAdapter;

    @Override
    protected Object getLayout() {
        MLog.e("R.layout.fragment_ranking_list");

        return R.layout.fragment_ranking_list;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        initView();
        initData();

    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);

    }

    @Override
    protected void initData() {
        //官方榜单
        Map<String, String> map = new HashMap<>();
        map.put("official", "1");//官方榜单
        NetworkManager.getInstance().getApiService().rankingList(map)
                .subscribeOn(Schedulers.io())
                .compose(this.bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new DefaultObserver<BaseResponses<RankingListBean>>(_mActivity) {
                    @Override
                    public void onSuccess(BaseResponses<RankingListBean> listBaseResponses) {

                         featuredGuanFangAdapter = new FeaturedGuanFangAdapter(_mActivity);
                        featuredGuanFangAdapter.getItems().addAll(listBaseResponses.getData().getItems());
                        binding.rvGuanFang.setLayoutManager(new GridLayoutManager(_mActivity, 2));
                        binding.rvGuanFang.setAdapter(featuredGuanFangAdapter);
                        DividerItemDecoration divider = new DividerItemDecoration();
                        divider.setDividerLookup(new DividerItemDecoration.DividerLookup() {
                            @Override
                            public Divider getVerticalDivider(int position) {
                                return new Divider.Builder()
                                        .size(20)
                                        .color(Color.TRANSPARENT)
                                        .build();
                            }

                            @Override
                            public Divider getHorizontalDivider(int position) {
                                return new Divider.Builder()
                                        .size(20)
                                        .color(Color.TRANSPARENT)
                                        .build();
                            }
                        });
                        binding.rvGuanFang.addItemDecoration(divider);
                        featuredGuanFangAdapter.setOnItemClickListener(new BaseBindingAdapter.OnItemClickListener<RankingListBean.ItemsDTO>() {
                            @Override
                            public void onItemClick(int position) {
                                EventBus.getDefault().post(new RanKingListDetailsEBBean(featuredGuanFangAdapter.getItems().get(position)));
                            }
                        });
                        initListener();
                    }
                });
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initListener() {
        binding.ivBang1.setOnClickListener(this);
        binding.ivBang2.setOnClickListener(this);
        binding.ivBang3.setOnClickListener(this);
        binding.ivBang4.setOnClickListener(this);
        binding.ivBang5.setOnClickListener(this);
        binding.ivBang6.setOnClickListener(this);
        binding.ivBang7.setOnClickListener(this);
        binding.ivBang8.setOnClickListener(this);
        binding.ivBang9.setOnClickListener(this);
        binding.ivBang10.setOnClickListener(this);
        binding.ivBang11.setOnClickListener(this);
        binding.ivBang12.setOnClickListener(this);
        binding.ivBang13.setOnClickListener(this);
        binding.ivBang14.setOnClickListener(this);
        binding.ivBang15.setOnClickListener(this);
    }

    public static RankingListFragment newInstance() {

        Bundle args = new Bundle();

        RankingListFragment fragment = new RankingListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onClick(View view) {
        EventBus.getDefault().post(new RanKingListDetailsEBBean(featuredGuanFangAdapter.getItems().get(3)));
    }
}